package com.ubaer.zsycz.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;

import com.lsw.shareUser.ShareUserInfoUtil;
import com.lsw.util.DeviceUtil;
import com.lsw.util.Helper;
import com.lsw.util.StatusBarCompat;
import com.lsw.util.StringUtil;
import com.ubaer.zsycz.R;


/**
 * 启动页
 *
 * @author lsw
 */
public class StartActivity extends Activity {
    private Context context;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);
        context = this;
        SetTranslanteBar();
        setContentView(R.layout.activity_start);
        Helper.statusBarLightMode(this);
        initViews();
    }

    /**
     * 沉浸状态栏（4.4以上系统有效）
     */
    protected void SetTranslanteBar() {
        StatusBarCompat.translucentStatusBar(this);
    }

    private ImageView imageView;

    /**
     * 运行渐变动画
     *
     * @param v
     */
    private void loadAnimation(View v) {
        AlphaAnimation aa = new AlphaAnimation(0.3f, 1.0f);
        // 动画时长
        aa.setDuration(3 * 1000);
        v.setAnimation(aa);
        aa.setAnimationListener(new AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                startAct();
            }
        });
    }

    /**
     * 转到相应的页面
     */
    private void startAct() {
        PackageInfo info = DeviceUtil.getMPackageInfo(this);
        int versionCode = info.versionCode;
        int saveVersion = ShareUserInfoUtil.getInstance(context).getInt(ShareUserInfoUtil.SP_VERSION, 0);
//        if (saveVersion < versionCode) {
//            // 跳转到引导页
//            startActivity(new Intent(this, GuiderActivity.class));
//            finish();
//        } else {
        if (StringUtil.isNullOrEmpty(ShareUserInfoUtil.getInstance(this).getString(ShareUserInfoUtil.USER_TOKEN, ""))) {
            // 跳转到登录页
            startActivity(new Intent(this, LoginActivity.class));
            finish();
        } else {
            openMain();
        }
//        }
    }

    protected void initViews() {
        imageView = findViewById(R.id.imageview);
        loadAnimation(imageView);
    }

    /**
     * 进入主页
     */
    public void openMain() {
        Intent intent = new Intent(context, MainActivity.class);
        startActivity(intent);
        ((Activity) context).finish();
    }

}
